#include "single_linked.h"
#include <stdlib.h>
#include <time.h>

SINGLE_LINKED_NODE_T* create_linked_tanks(void)
{
    // 随机种子
    srand(time(NULL));

    SINGLE_LINKED_NODE_T *pHead = NULL, *pPrev = NULL;

    // 一打坦克
    for(int i=0;i<12;i++){
        SINGLE_LINKED_NODE_T *pNode = malloc(sizeof(SINGLE_LINKED_NODE_T));
        pNode->tank.nTankNo = i+1;
        pNode->tank.nAmmo = rand() % 36;
        pNode->pNext = NULL;

        if(i == 0) pHead = pNode;
        else pPrev->pNext = pNode;
        
        pPrev = pNode;
    }

    return pHead;
}

int destroy_linked_tanks(SINGLE_LINKED_NODE_T *pHead)
{
    int count = 0;
    SINGLE_LINKED_NODE_T *pNode = pHead;
    while(pNode != NULL)
    {
        SINGLE_LINKED_NODE_T *pNext = pNode->pNext;
        free(pNode);
        count++;
        
        pNode = pNext;
    }

    return count;
}